package com.meiyou.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.meiyou.pojo.MeiyouSetting;
import com.meiyou.pojo.ShuJu;
import com.meiyou.pojo.ShuJuList;
import com.meiyou.service.ShuJuService;
import com.meiyou.utils.DateUtils;
import com.meiyou.utils.Msg;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Controller
@Api(value = "数据统计接口", tags = { "数据统计接口" })
@RequestMapping("/shuju")
public class ShujuController {

	@Autowired
	ShuJuService shujuService;
	
	//修改邀请用户注册获得金币数量
	@RequestMapping(value="/updateFXgetMeijin",method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value="修改邀请用户注册获得金币数量")
	public Msg updateFXgetMeijin(String quantity) {
		shujuService.updateFXgetMeijin(quantity);
		return Msg.success();
	}
	
	//修改用户发布任务诚意金
	@RequestMapping(value="/updateCYJgetMeijin",method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value="修改邀请用户注册获得金币数量")
	public Msg updateCYJgetMeijin(String quantity) {
		shujuService.updateCYJgetMeijin(quantity);
		return Msg.success();
	}
	
	//修改发布金
	@RequestMapping(value="/updateFBJgetMeijin",method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value="修改邀请用户注册获得金币数量")
	public Msg updateFBJgetMeijin(String quantity) {
		shujuService.updateFBJgetMeijin(quantity);
		return Msg.success();
	}
	
	//修改提现收费率
	@RequestMapping(value="/updateTXgetMeijin",method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value="修改邀请用户注册获得金币数量")
	public Msg updateTXgetMeijin(String quantity) {
		shujuService.updateTXgetMeijin(quantity);
		return Msg.success();
	}
	
	//修改置顶费
	@RequestMapping(value="/updateZDgetMeijin",method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value="修改邀请用户注册获得金币数量")
	public Msg updateZDgetMeijin(String quantity) {
		shujuService.updateZDgetMeijin(quantity);
		
		return Msg.success();
	}
	
	//修改通话费
	@RequestMapping(value="/updateTHgetMeijin",method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value="修改邀请用户注册获得金币数量")
	public Msg updateTHgetMeijin(String quantity) {
		shujuService.updateTHgetMeijin(quantity); 
		return Msg.success();
	}
	
	//获取所有基础数据
	@RequestMapping(value="/getalldata",method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value="修改邀请用户注册获得金币数量")
	public Msg getalldata() {
		List<MeiyouSetting> list = shujuService.getalldata();
		return Msg.success().add("list",list);
	}
	
	//所有数据
	@RequestMapping(value="/getAllShujuByDate",method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value="根据条件查询数据")
	public Msg getAllShujuByDate(String condition) {
		//判断要查询的条件修改date格式
		String date = null; 
		ShuJu shuju = new ShuJu();
		Calendar cal=Calendar.getInstance();
		if("今天".equals(condition)) {
			Date time = new Date();
			SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
			date = sf.format(time);
			shuju = shujuService.getAllShujuByDate(date);
			System.out.println("今天："+date);
		}
		if("昨天".equals(condition)) {
			cal.add(Calendar.DATE,-1);
			Date time=cal.getTime();
			SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
			date = sf.format(time);
			shuju = shujuService.getAllShujuByDate(date);
			System.out.println("昨天："+date);
		}
		if("本周".equals(condition)) {
			List<ShuJu> list = new ArrayList<ShuJu>();
			String[] dates = new String[7];
			Date starttime = DateUtils.getBeginDayOfWeek();
			SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
			String startdate = sf.format(starttime);
			dates[0] = startdate;
		    for(int i=1;i<7;i++) {
		    	cal.setTime(starttime);
		        cal.add(Calendar.DATE,+i);
		        Date time=cal.getTime();
				date = sf.format(time);
				dates[i] = date;
		    }
		    for(int i=0;i<dates.length;i++) {
		    	shuju = shujuService.getAllShujuByDate(dates[i]);
		    	list.add(shuju);
		    	System.out.println(dates[i]);
		    }
		    int volumeNumber = 0;
	    	double fenxiangJin = 0;
	    	int needData = 0;
	    	double postReward = 0;
	    	double chenyijin = 0;
	    	double pingtaifei = 0;
	    	double chongzhi = 0;
	    	double tixian = 0;
	    	double shipingfei = 0;
	    	int baominshu = 0;
		    for(ShuJu s:list) {
		    	System.out.println(s);
		    	volumeNumber = volumeNumber + Integer.parseInt(s.getVolumeNumber());
		    	fenxiangJin = fenxiangJin + Double.parseDouble(s.getFenxiangJin());
		    	needData = needData + Integer.parseInt(s.getNeedData());
		    	postReward = postReward + Double.parseDouble(s.getPostReward());
		    	chenyijin = chenyijin + Double.parseDouble(s.getChenyijin());
		    	pingtaifei = pingtaifei + Double.parseDouble(s.getPingtaifei());
		    	chongzhi = chongzhi + Double.parseDouble(s.getChongzhi());
		    	tixian = tixian + Double.parseDouble(s.getTixian());
		    	shipingfei = shipingfei + Double.parseDouble(s.getShipingfei());
		    	baominshu = baominshu + Integer.parseInt(s.getBaominshu());
		    }
		    shuju.setBaominshu(baominshu+"");
		    shuju.setChenyijin(chenyijin+"");
		    shuju.setChongzhi(chongzhi+"");
		    shuju.setFenxiangJin(fenxiangJin+"");
		    shuju.setNeedData(needData+"");
		    shuju.setPingtaifei(pingtaifei+"");
		    shuju.setPostReward(postReward+"");
		    shuju.setShipingfei(shipingfei+"");
		    shuju.setTixian(tixian+"");
		    shuju.setVolumeNumber(volumeNumber+"");
		}
		if("本月".equals(condition)) {
			Date time = new Date();
			SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM");
			date = sf.format(time);
			shuju = shujuService.getAllShujuByDate(date);
			System.out.println("本月："+date);
		}
		if("上月".equals(condition)) {
			cal.add(Calendar.MONTH,-1);
			Date time=cal.getTime();
			SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM");
			date = sf.format(time);
			shuju = shujuService.getAllShujuByDate(date);
			System.out.println("上月："+date);
		}
		if("本年".equals(condition)) {
			Date time = new Date();
			SimpleDateFormat sf = new SimpleDateFormat("yyyy");
			date = sf.format(time);
			shuju = shujuService.getAllShujuByDate(date);
			System.out.println("本年："+date);
		}
		if("全部".equals(condition)) { 
			shuju = shujuService.getAllShujuByDate(null);
		}
		return Msg.success().add("shuju", shuju); 
	}
	
	//仅查询最近7天所有数据
	@RequestMapping(value="/getThisWeekShujuByDate",method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value="仅查询本周所有数据")
	public Msg getThisWeekShujuByDate() {
		int[] volumeNumber = new int[7];  //注册总数
		
		double[] fenxiangJin = new double[7];   //分享金币
		
		double[] needData = new double[7];     //总发需求数
		
		double[] postReward = new double[7];    //悬赏金币
		
		double[] chenyijin = new double[7];     //诚意金币
		
		double[] pingtaifei = new double[7];    //平台费收益
		
		double[] chongzhi = new double[7];    //充值金币数
		
		double[] tixian = new double[7];    //提现金币数
		
		double[] shipingfei = new double[7]; //视频收费
		
		int[] baominshu = new int[7];  //报名人数

		String date = null; 
		ShuJu shuju = new ShuJu();
		ShuJuList shujuList = new ShuJuList();
		Calendar cal=Calendar.getInstance();
		
//	/	List<ShuJu> list = new ArrayList<ShuJu>();
		String[] dates = new String[7];
		Date endtime = new Date();
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
		String enddate = sf.format(endtime);
		dates[6] = enddate;
	    for(int i=1;i<7;i++) {
	    	cal.setTime(new Date());
	        cal.add(Calendar.DATE,-i);
	        Date time=cal.getTime();
			date = sf.format(time);
			dates[6-i] = date;
	    }
	    for(int i=0;i<dates.length;i++) {
	    	System.out.println(dates[i]);
	    }
	    for(int i=0;i<dates.length;i++) {
	    	shuju = shujuService.getAllShujuByDate(dates[i]);
	    	volumeNumber[i] = Integer.parseInt(shuju.getVolumeNumber());
	    	fenxiangJin[i] = Double.parseDouble(shuju.getFenxiangJin());
	    	needData[i] = Double.parseDouble(shuju.getNeedData());
	    	postReward[i] = Double.parseDouble(shuju.getPostReward());
	    	chenyijin[i] = Double.parseDouble(shuju.getChenyijin());
	    	pingtaifei[i] = Double.parseDouble(shuju.getPingtaifei());
	    	chongzhi[i] = Double.parseDouble(shuju.getChongzhi());
	    	tixian[i] = Double.parseDouble(shuju.getTixian());
	    	shipingfei[i] = Double.parseDouble(shuju.getShipingfei());
	    	baominshu[i] = Integer.parseInt(shuju.getBaominshu()); 
	    	//list.add(shuju);
	    }
	    shujuList.setVolumeNumber(volumeNumber);
	    shujuList.setBaominshu(baominshu);
	    shujuList.setChenyijin(chenyijin);
	    shujuList.setChongzhi(chongzhi);
	    shujuList.setFenxiangJin(fenxiangJin);
	    shujuList.setNeedData(needData);
	    shujuList.setPingtaifei(pingtaifei);
	    shujuList.setPostReward(postReward);
	    shujuList.setShipingfei(shipingfei);
	    shujuList.setTixian(tixian); 
	    shujuList.setDates(dates); 
		return Msg.success().add("shujuList", shujuList); 
	}
}
